# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
def dbHandle():
    conn = pymysql.connect(
        host = "localhost",
        user = "root",
        passwd = "root",
        charset = "utf8",
        use_unicode = False
    )
    return conn

class QiubaiJokePipeline:
    def process_item(self, item, spider):
        return item
class insetr_data_mysql(object):
    def process_item(self, item, spider):
        dbObject = dbHandle()
        cursor = dbObject.cursor()
        cursor.execute("USE test")
        sql_='insert into qb_data_test(author,context,stars) values(%s,%s,%s) '
        item['context']=item['context'].replace('<br>','').replace('<span>','').replace('</span>','')
        try:
            cursor.execute(sql_,(item['author'],item['context'],item['stars']))
            cursor.connection.commit()
        except BaseException as e:
            print(e)
            dbObject.rollback()
        return item